var uploader = WebUploader.create({
    // 选完文件后，是否自动上传。
    auto: true,
    // swf文件路径
    swf: 'Uploader.swf',
    // 文件接收服务端。
    server: '/ajax/uploadfile/uploadimg?filedir=ck',
    // 选择文件的按钮。可选。
    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
    pick: '#filePicker',
    // 只允许选择图片文件。
    accept: {
        title: 'Images',
        extensions: 'gif,jpg,jpeg,bmp,png',
        mimeTypes: 'image/*'
    },
    duplicate: true
});
var fileImg = new Array();
// 文件上传成功，给item添加成功class, 用样式标记上传成功。
uploader.on('uploadSuccess', function (file, response) {
    if (response.code != 1) {
        var $imguploadErrorSrc = '/base/lib/webuploader/upload/uploadErrorImg.png';
        errorText(file, response);
    } else {
        var data = response.data;
        var hid_images = $("#hid_images").val();
        hid_images += "," + data;
        $("#hid_images").val(hid_images);
        var img = addFiles(data + "_388x388");
        fileImg.push(img);
        $("#filelist").after(img);
    }
});

function addFiles(data) {
    // var img = "<span style='padding:20px 0 0 10px'>" +
    //     "<a href='" + data + "' target='_blank'>" +
    //     "<img src='" + data + "' width='200' height='200'/>" +
    //     "</a></span>";
    var img = "<div class=\"imgShow\">"
    img += "<a href='" + data + "' target='_blank'>";
    img += "<img src='" + data + "'></a>";
    img += "<div class='btnShow'>";
    img += "<span class='img-btn' key-btn='mainImg'>设为主图</span>";
    img += "<span class='img-btn' key-btn='deleteImg'>删除</span>";
    img += "</div>";
    img += "</div>";
    return img;
}

function errorText(file, reason) {
    var $li = $('#' + file.id),
        $error = $li.find('p.error');
    // 避免重复创建
    if (!$error.length) {
        $error = $('<p class="error"></p>').appendTo($li);
    }
    $error.text('上传失败');
}

$(document).ready(function () {
    initImgList();
    $("#queueList").on("click", ".img-btn", function () {
        var obj = $(this);
        var type = obj.attr("key-btn");
        var curImgUrl = obj.parent().parent().find("a").find("img").attr("src");
        var hid_images = $("#hid_images").val();
        curImgUrl = curImgUrl.replace("_388x388", "")
        if (type == "mainImg") {
            var aryImg = [];
            aryImg.push(curImgUrl);
            $("#hid_mainImg").val(curImgUrl);
            $.each(hid_images.split(','), function (i, val) {
                if (val != '' && val != curImgUrl) {
                    aryImg.push(val)
                }
            });
            $("#hid_images").val(',' + aryImg.join(','));
            initImgList();
        } else if (type == "deleteImg") {
            var aryImg = [];
            $.each(hid_images.split(','), function (i, val) {
                if (val != '' && val != curImgUrl) {
                    aryImg.push(val)
                }
            });
            $("#hid_images").val(aryImg.join(','));
            if (aryImg.length == 0) {
                $("#queueList").attr("style", "width:660px;");
            }
            obj.parent().parent().remove();
        } else {
            alert("error click");
        }
    });
});

/*初始化图片*/
function initImgList() {
    var hid_images = $("#hid_images").val();
    $("#queueList").find("div[class='imgShow']").remove();
    var lisImg = hid_images.split(',');
    if (hid_images.trim() != '' && lisImg.length > 0) {
        // $("#queueList").attr("style","width:660px;border: 1px solid #e7eaec;");
        $("#queueList").css({
            "width": "660px",
            "border": "1px solid #e7eaec"
        });
    } else {
        $("#queueList").css("width", "660px");
    }
    for (var i = lisImg.length; i >= 0; i--) {
        var val = lisImg[i];
        if (val != '' && val != undefined) {
            var img = addFiles(val + '_388x388');
            $("#filelist").after(img);
        }
    }
}

